package com.gxar.marketdelivery.interfaces.web.result;

import com.gxar.marketdelivery.shared.meta.exception.IBizException;
import lombok.Data;

@Data
public class APIResult<T> {

    private Integer status;

    private String message;

    private T data;

    public APIResult() {
        this(IResponseStatusMsg.APIEnum.SUCCESS);
    }

    public APIResult(IResponseStatusMsg responseStatusMsg) {
        this.status = responseStatusMsg.getCode();
        this.message = responseStatusMsg.getMessage();
    }

    public APIResult(Integer status, String message) {
        this.status = status;
        this.message = message;
    }

    public APIResult(T data) {
        this();
        this.data = data;
    }

    public APIResult(Exception e) {
        if (e instanceof IBizException) {
            IBizException bizException = (IBizException) e;
            this.status = bizException.getCode();
            this.message = bizException.getMessage();
        } else {
            this.status = IResponseStatusMsg.APIEnum.SERVER_ERROR.getCode();
            this.message = e.getMessage();
        }
    }

    public APIResult(Exception e, T data) {
        this(e);
        this.data = data;
    }

    public static APIResult success() {
        return new APIResult(IResponseStatusMsg.APIEnum.SUCCESS);
    }
}
